<% title "SQL Editor" %>

<%= form_tag :sql_execute do %>

  <div class="large-12">
    <div class='left'>
      <h3>Execute SQL</h3>
    </div>

    <div class='left sql_exp_col'>
      <%= render '/rails_db/shared/exp_col' %>
    </div>
  </div>

  <div class='clear'></div>

  <% if @sql_query.valid? %>
    <% if @sql_query.data.is_error? %>
      <div class="large-12">
        <div class='alert-box alert'>
          <%= @sql_query.data.error.message %>
        </div>
      </div>
    <% end %>
  <% end %>

  <div class="large-12">
    <textarea name='sql' id="sql" cols="120" rows="60"><%= @sql %></textarea>
  </div>

  <div class="large-12">
    <div class='panel execute_panel'>
      <p>
        <span class="radius secondary label">* be careful with your SQL. You may corrupt your DB. Make sure you have DB backup if making modifications.</span>
        <br/>
        <span class="radius secondary label">** you can autocomplete your SQL (Esc, Ctrl-Space or Ctrl)</span>
      </p>
      <%= submit_tag raw("&rarr; Execute"), class: 'button small' %>
      &nbsp;
      &mdash;
      &nbsp;
      <%= link_to raw("#{fa_icon('undo')} Reset"), :sql, data: {confirm: "Are you sure?"}, confirm: "Are you sure?" %>
      &nbsp;
      &mdash;
      &nbsp;
      <a href="#" data-reveal-id="history_modal" id='history_modal_link'><%= fa_icon('bars') %> History</a></span>
      <%= render 'history' %>
    </div>
  </div>

<% end %>

<% if @sql_query.valid? %>
  <%= render 'results', sql_query: @sql_query, sql: @sql %>
<% end %>

<script type="text/javascript">
  window.onload = function() {
    init_sql_editor("<%= RailsDb::Database.adapter.mime %>", <%= raw db_hint_options.to_json %>);
  };
</script>
